<template>
    <view class="address_box">
        <view class="box-top">
            <view class="address_detail">
                <view class="add-address">
                    <view class="add-button">
                        <van-button type="info" size="small" color="#FF9900" @tap="handleGoAdd">添加地址</van-button>
                    </view>

                </view>
                <view v-for="(item,index) of addressList" :key="index" class="address-item">
                    <view class="address-name-mobile" @tap="chooseAddress(item)">
                        <view><span>{{item.realname}}</span></view>
                        <view> <span>{{item.mobile}}</span></view>
                    </view>
                    <view class="address" @tap="chooseAddress(item)">
                        {{item.province}}{{item.city}}{{item.district}}{{item.house_number}}
                    </view>
                    <view class="address_button">
                        <view class="address-default">
                            <van-radio-group :value="default_id" @change="onChange">
                                <van-radio :name="item.id" checked-color="#07c160">设为默认</van-radio>
                            </van-radio-group>

                        </view>
                        <view class="button-top">
                            <view class="button-edit"><van-button type="info" size="small" :data-id="item.id" @tap="handleGoEdit($event)">编辑</van-button></view>
                            <view class="button-delete"><van-button type="info" size="small" :data-id="item.id" @tap="delAddress($event)">删除</van-button></view>
                        </view>
                        <view>

                        </view>

                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        name: "addressList",
        data() {
            return {
                default_id:'',
                params: {},
                addressList: [],
                is_refresh:false
            }
        },
        onshow:function(){
            if(this.is_refresh){
                this.getAddressList();
            }
        },
        mounted() {
            this.getAddressList();
        },
        methods: {
           async getAddressList() {
               let otherAddress = await  this.$apis.getAddressList({access_token:''})
                if(otherAddress.data.addressList.length>0){
                    this.addressList=otherAddress.data.addressList
                    this.addressList.forEach((item)=>{
                        if(item.is_default===1){
                            this.default_id=item.id
                            return
                        }

                    })
                }
                this.is_refresh=true
            },
            async onChange(event) {
                if(event.detail!==this.default_id){
                    //先处理默认地址请求
                    uni.showLoading({});
                    let resData=await this.$apis.setDefaultAddress({access_token: '',id:this.default_id})
                    if(resData.code===0){
                        this.default_id=event.detail;
                    }
                    uni.hideLoading();

                }

            },
            handleGoAdd() {
                uni.redirectTo({
                    url: "/pages/user/addAddress"
                })
            },
            handleGoEdit(event) {
                // this.$router.push({
                //     route:this.$routesConfig.editAddress,
                //     query:{
                //         id:event.currentTarget.dataset.id,
                //     }
                // })
                uni.redirectTo({
                    url: "/pages/user/editAddress?id="+event.currentTarget.dataset.id
                })
            },
            async delAddress(event) {
                uni.showLoading({});
                let resData=await this.$apis.delAddress({id:event.currentTarget.dataset.id})
                uni.hideLoading();
                if(resData.code===0){
                    uni.showToast({
                        title:"删除成功",
                        icon:"success",
                    })
                    setTimeout(()=>{
                        uni.redirectTo({
                            url: "/pages/user/addressList"
                        })
                    },1000)
                }


            },
            chooseAddress(item){
               this.$store.commit('CHANGEADDRESSID',item.id)
                uni.navigateBack({
                    delta: 1 // 代表返回上 1 页
                })
            }
        },
        components: {}
    }
</script>

<style lang="less" scoped>
    .address_box{
     display: flex;
     flex-direction: column;
     align-items: center;
    }
    .box-top{
        width: 90%;
    }
    .address_detail{
        display: flex;
        flex-direction: column;
    }
    .button-top{
        display: flex;
        flex-direction: row;
        position: relative;
        right: -90rpx;
    }
    .address_button{
        display: flex;
        justify-content: space-between;
        flex-direction: row;
        margin-top: 15rpx;
        border-top: solid #cccccc 1px ;
        padding-top: 10rpx;
    }
    .address-item{
        padding:35rpx;
        padding-bottom: 20rpx;
        font-size: 28rpx;
        margin-top: 25rpx;
        border: solid #cccccc 1px
    }
    .add-address{
        display: flex;
        justify-content: flex-end;
        margin-top: 20rpx;
        .add-button{
            position: relative;
            right: 0rpx;
        }
    }
    .address-name-mobile{
        display: flex;
        justify-content: space-between;
    }
    .address{
        margin-top: 40rpx;
    }
    .address-default{
        margin-top: 10rpx;
    }
    .button-delete{
        margin-left: 5rpx;
    }
</style>
